package com.easy.sk.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.SeckillResult;
import com.easy.common.service.ISeckillResultService;
import com.easy.sk.system.mapper.SeckillResultMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class SeckillResultServiceImpl extends ServiceImpl<SeckillResultMapper, SeckillResult> implements ISeckillResultService {

    @Override
    public SeckillResult getResultById(String id) {
        return super.getOne(new QueryWrapper<SeckillResult>().eq("id", id));
    }

    @Override
    public List<SeckillResult> getResultsByUserId(int userId) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillResult::getUser_id, userId);
        queryWrapper.eq(SeckillResult::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public List<SeckillResult> getResultsBySeckillGoodsId(int seckillGoodsId) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillResult::getSeckill_goods_id, seckillGoodsId);
        queryWrapper.eq(SeckillResult::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public List<SeckillResult> getResultsByActivityId(int activityId) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillResult::getActivity_id, activityId);
        queryWrapper.eq(SeckillResult::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public int addResult(SeckillResult result) {
        // 确保删除状态默认为0(未删除)
        if (result.getIs_del() == null) {
            result.setIs_del(0);
        }
        return super.save(result) ? 1 : 0;
    }

    @Override
    public int updateResult(SeckillResult result) {
        return super.updateById(result) ? 1 : 0;
    }

    @Override
    public int deleteResult(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public List<SeckillResult> getAll(SeckillResult result) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillResult::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<SeckillResult> getPage(SeckillResult result, IPage<SeckillResult> page) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        if (result.getId() != null){
            queryWrapper.eq(SeckillResult::getId, result.getId());
        }
        queryWrapper.like(result.getUser_id() != null, SeckillResult::getUser_id, result.getUser_id());
        queryWrapper.like(result.getSeckill_goods_id() != null, SeckillResult::getSeckill_goods_id, result.getSeckill_goods_id());
        queryWrapper.like(result.getActivity_id() != null, SeckillResult::getActivity_id, result.getActivity_id());
        queryWrapper.like(result.getResult() != null, SeckillResult::getResult, result.getResult());
        queryWrapper.eq(SeckillResult::getIs_del, 0);
        return super.page(page, queryWrapper);
    }
}
